using System;
using System.IO;
using Enums;
using Tools.Singleton;
using UnityEngine;

// 文件读写类
// 这个类不管读或写都是string（json）类型的数据，注意这个读写类是给自己做数据持久化用的，而不是做IO流下载的那种
// 其中枚举自个新建或就该即可。

namespace Tools.File
{
    public class FileManager : UnitySingleton<FileManager>
    {
        private FileInfo _fileConfig;
        private FileInfo _fileEquipmentBindData;
        private FileInfo _fileLoginData;

        /// <summary>
        /// 根据枚举获取文件全名
        /// </summary>
        /// <param name="enumLocalFile"></param>
        /// <returns></returns>
        private string GetFileFullName(EnumLocalFile enumLocalFile)
        {
            string fileName = "";
            switch (enumLocalFile)
            {
                case EnumLocalFile.Config:
                    fileName = "Config.json";
                    break;
                case EnumLocalFile.EquipmentBindData:
                    fileName = "EquipmentBindData.json";
                    break;
                case EnumLocalFile.LoginData:
                    fileName = "LoginData.json";
                    break;
                case EnumLocalFile.PatrolList:
                    fileName = "PatrolList.json";
                    break;
                case EnumLocalFile.PatrolDetails:
                    fileName = "PatrolDetails.json";
                    break;
                case EnumLocalFile.InputBindQRCode:
                    fileName = "InputBindQRCode.json";
                    break;
            }
            return Application.persistentDataPath + "/" + fileName;
        }
        
        /// <summary>
        /// 写入本地文件
        /// </summary>
        /// <param name="content">文件内容</param>
        /// <param name="enumLocalFile">文件</param>
        /// <param name="isAppendContent">是否追加内容</param>
        public void WriteLocalData(string content, EnumLocalFile enumLocalFile,bool isAppendContent = false)
        {
            string fileName = "";
            try
            {
                if (isAppendContent)
                {
                    content += ReadLocalData(enumLocalFile);
                }
                fileName = GetFileFullName(enumLocalFile);
                System.IO.File.WriteAllText(fileName, content);
            }
            catch (Exception e)
            {
                Debug.LogError($"写入配置文件{fileName}出错,原因:{e.Message}");
            }
        }
        public void WriteLocalData(byte[] content, EnumLocalFile enumLocalFile)
        {
            string fileName = "";
            try
            {
                fileName = GetFileFullName(enumLocalFile);
                System.IO.File.WriteAllBytes(fileName, content);
            }
            catch (Exception e)
            {
                Debug.LogError($"写入配置文件{fileName}出错,原因:{e.Message}");
            }
        }

        /// <summary>
        /// 读取本地文件
        /// </summary>
        /// <param name="enumLocalFile">文件</param>
        /// <returns></returns>
        public string ReadLocalData(EnumLocalFile enumLocalFile)
        {
            string fileName = "";
            try
            {
                fileName = GetFileFullName(enumLocalFile);
                if (System.IO.File.Exists(fileName) == false)
                {
                    System.IO.File.Create(fileName);
                    return null;
                }
                return System.IO.File.ReadAllText(fileName);
            }
            catch (System.Exception e)
            {
                Debug.LogError($"读取配置文件{fileName}出错,原因:{e.Message}");
                return null;
            }
        }
    }
}
// 举个栗子
namespace Tools.File.Test
{
    public class A 
    {
        public void B()
        {
            // 读取本地配置文件Config.json
            string configString = FileManager.Instance.ReadLocalData(EnumLocalFile.Config);

            // 写入本地配置文件Config.json
            FileManager.Instance.WriteLocalData(configString, EnumLocalFile.Config);
        }
        
    }
}